package edu.mapi.ir.viewer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

import javax.swing.JPanel;

import edu.mapi.ir.gui.GuiUtils;
import edu.mapi.ir.gui.RoundGradientPaint;

/**
 * @author ZP
 */
public class MousePlayer {
	
	private static Color[] colors = new Color[] {			
			Color.blue.darker(),
			Color.green.darker(),
			Color.orange.darker(),					
			Color.red.darker(),	
			Color.cyan.darker(),
			Color.magenta.darker(),
			Color.black
	};
	
	public static int num = 0;
	private String playerName = "Player "+num;
	private Point2D position = new Point2D.Double(0,0);
	public double[] distanceSensors = new double[4];	
	private double direction = 0;
	private Color color = colors[num++%colors.length];
	
	public MousePlayer(GridPosition position) {
		this.position = position.position;
		this.direction = position.direction;
	}
	
	public MousePlayer(Point2D initialPos) {
		this.position = initialPos;
		this.direction = 0;
	}
	
	public void paintMouse2(Graphics2D g, double zoom) {
		Ellipse2D body = new Ellipse2D.Double(-zoom/2,-zoom/2,zoom,zoom);
		Ellipse2D left_eye = new Ellipse2D.Double(-zoom/4, -zoom/3, zoom/10, zoom/10);
		Ellipse2D right_eye = new Ellipse2D.Double(zoom/6, -zoom/3, zoom/10, zoom/10);
		double ang = Math.toRadians(direction-90);	
		
		//Point2D focal = new Point2D.Double(-zoom/3, zoom/4);
		Point2D focal = new Point2D.Double(-Math.cos(ang)*(zoom/2),-Math.sin(ang)*(zoom/2));
		Point2D radius = new Point2D.Double(zoom/1.5, zoom/1.5);				
		
		AffineTransform trans = g.getTransform();
		
		double sensorPos[] = new double[] {0, -Math.PI/3, Math.PI/3, Math.PI};
		
		for (int i = 0; i < 4; i++) {
			if (distanceSensors[i] > 0) {
				g.setTransform(trans);
				
				g.rotate(sensorPos[i]);
				g.translate(0, -0.5 * zoom);
				double distA = 1.0 / (distanceSensors[i]+0.1);
				double distB = 1.0 / (distanceSensors[i]);
				double distC = 1.0 / (distanceSensors[i]) + 0.5;
				distA *= zoom;
				distB *= zoom;
				distC *= zoom;
				
				Arc2D.Double arcA = new Arc2D.Double(-distA, -distA, distA*2, distA*2, 60, 60.0,Arc2D.PIE);
				Arc2D.Double arcB = new Arc2D.Double(-distB, -distB, distB*2, distB*2, 60, 60.0,Arc2D.PIE);
				Arc2D.Double arcC = new Arc2D.Double(-distC, -distC, distC*2, distC*2, 60, 60.0,Arc2D.PIE);
				Area c = new Area(arcC);
				c.subtract(new Area(arcB));
				Area b = new Area(arcB);
				b.subtract(new Area(arcA));
				g.setColor(Color.black);
				g.draw(arcC);
				g.setColor(new Color(0,255,0,150));		
				g.fill(arcA);				
				g.setColor(new Color(255,255,0,150));
				g.fill(b);
				g.setColor(new Color(255,0,0,150));
				g.fill(c);
			}
		}
		
		g.setTransform(trans);
		
		Color c = color.brighter();
		g.setColor(new Color(c.getRed(),c.getGreen(),c.getBlue(), 100));				
		g.setStroke(new BasicStroke(2f));
		
		g.draw(new Ellipse2D.Double(-zoom/2-1,-zoom/2-1,zoom+2,zoom+2));
		
		g.setColor(Color.black);
		
		
		g.setPaint(new RoundGradientPaint((int)focal.getX(), (int)focal.getY(), Color.white, radius, Color.gray));
		g.fill(body);

		Area a = new Area(body);
		a.subtract(new Area(new Ellipse2D.Double(-zoom/2, -zoom, zoom, zoom)));
		g.setPaint(new RoundGradientPaint((int)focal.getX(), (int)focal.getY(), Color.white, radius, color.darker()));
		g.fill(a);
		
		g.setColor(color.brighter());
		
		g.fill(left_eye);
		g.fill(right_eye);
		
		g.setColor(Color.black);
		g.setStroke(new BasicStroke(1.f));
		
		g.drawLine(0, (int)-zoom/2, 0, (int)zoom/2);
		g.draw(body);
	}
	
	public void paintMouse(Graphics2D g, double zoom) {
		Ellipse2D body = new Ellipse2D.Double(-zoom/2,-zoom/2,zoom,zoom);
		Ellipse2D left_eye = new Ellipse2D.Double(-zoom/4, -zoom/3, zoom/10, zoom/10);
		Ellipse2D right_eye = new Ellipse2D.Double(zoom/6, -zoom/3, zoom/10, zoom/10);
		double ang = Math.toRadians(direction-90);	
		
		//Point2D focal = new Point2D.Double(-zoom/3, zoom/4);
		Point2D focal = new Point2D.Double(-Math.cos(ang)*(zoom/2),-Math.sin(ang)*(zoom/2));
		Point2D radius = new Point2D.Double(zoom/1.5, zoom/1.5);				
		
		AffineTransform trans = g.getTransform();
		
		double sensorPos[] = new double[] {0, -Math.PI/3, Math.PI/3, Math.PI};
		g.setColor(new Color(255,0,0,50));
		for (int i = 0; i < 4; i++) {
			if (distanceSensors[i] > 0) {
				g.setTransform(trans);
				
				g.rotate(sensorPos[i]);
				g.translate(0, -0.5 * zoom);
				double dist = 1.0 / distanceSensors[i];
				dist += 0.15;
				if (dist <= 0.3)
					continue;
				
				dist *= zoom;
				
				Arc2D.Double arc = new Arc2D.Double(-dist, -dist, dist*2, dist*2, 60, 60.0,Arc2D.PIE);				
				g.fill(arc);
				
			}
		}
		
		g.setTransform(trans);
		
		Color c = color.brighter();
		g.setColor(new Color(c.getRed(),c.getGreen(),c.getBlue(), 100));				
		g.setStroke(new BasicStroke(2f));
		
		g.draw(new Ellipse2D.Double(-zoom/2-1,-zoom/2-1,zoom+2,zoom+2));
		
		g.setColor(Color.black);
		
		
		g.setPaint(new RoundGradientPaint((int)focal.getX(), (int)focal.getY(), Color.white, radius, Color.gray));
		g.fill(body);

		Area a = new Area(body);
		a.subtract(new Area(new Ellipse2D.Double(-zoom/2, -zoom, zoom, zoom)));
		g.setPaint(new RoundGradientPaint((int)focal.getX(), (int)focal.getY(), Color.white, radius, color.darker()));
		g.fill(a);
		
		g.setColor(color.brighter());
		
		g.fill(left_eye);
		g.fill(right_eye);
		
		g.setColor(Color.black);
		g.setStroke(new BasicStroke(1.f));
		
		g.drawLine(0, (int)-zoom/2, 0, (int)zoom/2);
		g.draw(body);
		
		
		
		//System.out.println(System.currentTimeMillis()-curTime);			
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public Point2D getPosition() {
		return position;
	}

	public void setPosition(Point2D position) {
		this.position = position;
	}

	public double getDirection() {
		return direction;
	}

	public void setDirection(double direction) {
		this.direction = direction;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}
	
	public static void main(String[] args) {
		JPanel jp = new JPanel() {
			MousePlayer mp = new MousePlayer(new Point2D.Double(0,0));
			
			@Override
			public void paint(Graphics g) {
				super.paint(g);
				mp.distanceSensors[0] = 0.7;
				mp.distanceSensors[1] = 1.2;
				mp.distanceSensors[2] = 0.9;
				Graphics2D g2d = (Graphics2D)g;
				g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);				
				g2d.translate(getWidth()/2, 570);
				mp.paintMouse2(g2d, 230);
				
			}
		};
		jp.setBackground(Color.white);
		GuiUtils.testFrame(jp);
	}
	
}
